/*
* richBridge v1.2.6
* =======================
* Author: bester
*/
RICH = window.RICH || {};
RICH.Bridge = {
    //关闭webview
    getH5Close: function(refresh){
        if(is_android()){
            window.jsBridge.getH5Close(refresh);
        }
        if(is_iphone()){
            if(iosVersion < 8){
                window.jsBridge.getH5Close(refresh);
            }else{
                window.webkit.messageHandlers.getH5Close.postMessage(refresh);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getH5Close.postMessage(refresh);
        }
    },
    //回退webview
    getH5Back: function(){
        if(is_android()){
            window.jsBridge.getH5Back();
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getH5Back();
            }else{
                window.webkit.messageHandlers.getH5Back.postMessage('1');
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getH5Back.postMessage('1');
        }
    },
    //获取登录令牌
    getLoginToken: function(authType){
        if(is_android()){
            window.jsBridge.getLoginToken(authType);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getLoginToken(authType);
            }else{
                window.webkit.messageHandlers.getLoginToken.postMessage(authType);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getLoginToken.postMessage(authType);
        }
    },
    //换取有效的登录令牌
    getRightToken: function(authType,authValue){
        if(is_android()){
            window.jsBridge.getRightToken(authType,authValue);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getRightToken({'authType':authType,'authValue':authValue});
            }else{
                window.webkit.messageHandlers.getRightToken.postMessage({'authType':authType,'authValue':authValue});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getRightToken.postMessage({'authType':authType,'authValue':authValue});
        }
    },
    //工单状态更新
    getOrderUpdate: function(authType,authValue,accessToken,appId,bid,btype){
        if(is_android()){
            window.jsBridge.getOrderUpdate(authType,authValue,accessToken,appId,bid,btype);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getOrderUpdate({'authType':authType,'authValue':authValue,'accessToken':accessToken,'appId':appId,'bid':bid,'btype':btype});
            }else{
                window.webkit.messageHandlers.getOrderUpdate.postMessage({'authType':authType,'authValue':authValue,'accessToken':accessToken,'appId':appId,'bid':bid,'btype':btype});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getOrderUpdate.postMessage({'authType':authType,'authValue':authValue,'accessToken':accessToken,'appId':appId,'bid':bid,'btype':btype});
        }
    },
    //修改webview标题
    getH5ChangeTitle: function(titleMsg){
        if(is_android()){
            window.jsBridge.getH5ChangeTitle(titleMsg);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getH5ChangeTitle(titleMsg);
            }else{
                window.webkit.messageHandlers.getH5ChangeTitle.postMessage(titleMsg);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getH5ChangeTitle.postMessage(titleMsg);
        }
    },
    //更新角标
    setTabIconNum: function(num){
        if(is_android()){
            window.jsBridge.setTabIconNum(num);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.setTabIconNum(num);
            }else{
                window.webkit.messageHandlers.setTabIconNum.postMessage(num);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.setTabIconNum.postMessage(num);
        }
    },
    //错误上报
    reportErr: function(appId,uid,serId,action,btime,errs){
        if(is_android()){
            window.jsBridge.reportErr(appId,uid,serId,action,btime,errs);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.reportErr({'appId':appId,'uid':uid,'serId':serId,'action':action,'btime':btime,'errs':errs});
            }else{
                window.webkit.messageHandlers.reportErr.postMessage({'appId':appId,'uid':uid,'serId':serId,'action':action,'btime':btime,'errs':errs});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.reportErr.postMessage({'appId':appId,'uid':uid,'serId':serId,'action':action,'btime':btime,'errs':errs});
        }
    },
    //内部跳转(H5)
    goToWebview: function(url,title){
        if(is_android()){
            window.jsBridge.goToWebview(url,title);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.goToWebview({'url':url,'title':title});
            }else{
                window.webkit.messageHandlers.goToWebview.postMessage({'url':url,'title':title});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.goToWebview.postMessage({'url':url,'title':title});
        }
    },
    //获取当前位置
    getLatlng: function(){
        if(is_android()){
            window.jsBridge.getLatlng();
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getLatlng();
            }else{
                window.webkit.messageHandlers.getLatlng.postMessage('1');
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getLatlng.postMessage('1');
        }
    },
    //打开摄像头拍照
    openCapture: function(width){
        if(is_android()){
            window.jsBridge.openCapture(width);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.openCapture(width);
            }else{
                window.webkit.messageHandlers.openCapture.postMessage(width);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.openCapture.postMessage(width);
        }
    },
    //打开相册选图片
    chooseFile: function(width){
        if(is_android()){
            window.jsBridge.chooseFile(width);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.chooseFile(width);
            }else{
                window.webkit.messageHandlers.chooseFile.postMessage(width);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.chooseFile.postMessage(width);
        }
    },
    //录制短视频
    startMedia: function(time){
        if(is_android()){
            window.jsBridge.startMedia(time);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.startMedia(time);
            }else{
                window.webkit.messageHandlers.startMedia.postMessage(time);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.startMedia.postMessage(time);
        }
    },
    //android附件预览
    attachmentPreview: function(type,url){
        if(is_android()){
            window.jsBridge.attachmentPreview(type,url);
        }
    },
    //扫一扫
    scanQRCodeForText: function(){
        if(is_android()){
            window.jsBridge.scanQRCodeForText();
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.scanQRCodeForText();
            }else{
                window.webkit.messageHandlers.scanQRCodeForText.postMessage('1');
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.scanQRCodeForText.postMessage('1');
        }
    },
    //公文正文转邮件
    textTransferToMail: function(mailTitle,mailContent){
        if(is_android()){
            window.jsBridge.textTransferToMail(mailTitle,mailContent);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.textTransferToMail({'mailTitle':mailTitle,'mailContent':mailContent});
            }else{
                window.webkit.messageHandlers.textTransferToMail.postMessage({'mailTitle':mailTitle,'mailContent':mailContent});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.textTransferToMail.postMessage({'mailTitle':mailTitle,'mailContent':mailContent});
        }
    },
    //录制视频
    startMediaRecord: function(time){
        if(is_android()){
            window.jsBridge.startMediaRecord(time);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.startMediaRecord({'time':time});
            }else{
                window.webkit.messageHandlers.startMediaRecord.postMessage({'time':time});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.startMediaRecord.postMessage({'time':time});
        }
    },


    //打开离线照片或者视频  type：1 为照片  2为视频
    openOfflineFileList: function(type,width){
        if(is_android()){
            window.jsBridge.openOfflineFileList(type);
        }
        if( type == 1 ){
            if(is_iphone()){
                if(iosVersion < 8){  
                    window.jsBridge.chooseOfflinePhoto(width);
                }else{
                    window.webkit.messageHandlers.chooseOfflinePhoto.postMessage(width);
                }
            }
            if(is_iPad()){
                window.webkit.messageHandlers.chooseOfflinePhoto.postMessage(width);
            }
        }else if( type == 2 ){
            if(is_iphone()){
                if(iosVersion < 8){  
                    window.jsBridge.chooseOfflineVideo(width);
                }else{
                    window.webkit.messageHandlers.chooseOfflineVideo.postMessage(width);
                }
            }
            if(is_iPad()){
                window.webkit.messageHandlers.chooseOfflineVideo.postMessage(width);
            }
        }
    },

    //删除离线视频与照片
    deleteOfflineFile: function(filePath){
        if(is_android()){
            window.jsBridge.deleteOfflineFile(filePath);
        }
        
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.deleteOfflineFile(filePath);
            }else{
                window.webkit.messageHandlers.deleteOfflineFile.postMessage(filePath);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.deleteOfflineFile.postMessage(filePath);
        }
    },

    //离线视频缓存记录
    getMediaByFileName: function(fileName){
        if(is_android()){
            window.jsBridge.getMediaByFileName(fileName);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getMediaByFileName({'fileName':fileName});
            }else{
                window.webkit.messageHandlers.getMediaByFileName.postMessage({'fileName':fileName});
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getMediaByFileName.postMessage({'fileName':fileName});
        }
    },

    //修改webview顶部栏图片   app端顶部栏尺寸为750px*120px
    getH5TopImg: function(topImg){
        if(is_android()){
            window.jsBridge.getH5TopImg(topImg);
        }
        if(is_iphone()){
            if(iosVersion < 8){  
                window.jsBridge.getH5TopImg(topImg);
            }else{
                window.webkit.messageHandlers.getH5TopImg.postMessage(topImg);
            }
        }
        if(is_iPad()){
            window.webkit.messageHandlers.getH5TopImg.postMessage(topImg);
        }
    }
}
var ua = navigator.userAgent.toLowerCase();
console.log(ua)
var iosVersion = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
if(is_iphone()){
    iosVersion = parseInt(iosVersion[1], 10);
}
//判断环境
function is_android(){
    if(ua.match(/android/i)=="android"&&ua.match(/iemobile/i)!="iemobile"){
         return true;
    } else {
         return false;
    }
}
function is_iphone(){
    if(ua.match(/iphone/i)=="iphone"&&ua.match(/iemobile/i)!="iemobile"){
         return true;
    } else {
         return false;
    }
}
function is_iPad(){
    if(ua.match(/iPad/i)=="iPad"||ua.match(/ipad/i)=="ipad"){
         return true;
    } else {
         return false;
    }
}